Skip to content

Conversation

mu001999
Copy link
Contributor

@mu001999 mu001999 commented Oct 9, 2024

No description provided.

@rustbot
Copy link
Collaborator

rustbot commented Oct 9, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 9, 2024
return;
}
&STACK_TRACE.as_slice()[0..(depth as _)]
slice::from_raw_parts(&raw const STACK_TRACE as _, depth as _)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like neither of these fixes the underlying issue that we likely shouldn't be using a static mut here... It just side-steps the lint by using raw pointers which for some reason we don't lint on.

Copy link
Contributor Author

@mu001999 mu001999 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. But I didn't see other ways to implement this. And we can guarantee that using raw pointers here is correct, right?

Copy link
Contributor

@traviscross traviscross Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by using raw pointers which for some reason we don't lint on.

We don't lint against raw pointers because that's often what we're trying to push people toward (among other options). We lint against references because it's easier to get that wrong without realizing. E.g., it's fine to have aliasing pointers to some data as long as you're careful to not race the accesses, but it's never fine to have aliasing live mutable references. But taking potentially long-lived references to mutable statics makes it really easy to do that accidentally.

To make this particular case safer, I'd probably move STACK_TRACE into that let stack = unsafe { .. } block. Then it can't be directly accessed later while the reference to it via that slice is live, which is the main danger.

Probably I'd also expect this function to be unsafe extern "C" fn .. since there's a safety requirement here that must be upheld by callers (the function is not reentrant). Not sure if there's some practical reason preventing that here.

Assuming that safety invariant is upheld, the use of static mut here, as revised, seems fine to me in the context of what we were trying to achieve as a lang matter. This is plausibly the kind of use case that argues against deprecating static mut entirely (see #53639).

}

// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
#[allow(static_mut_refs)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check whether the library builds on stage 2? This should definitely still trigger the lint here since we haven't (afaict?) changed anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found no reference used in this function's body, so I removed it and just try to build on stage 2 on my linux device.
But I just found it needs #[cfg(all(target_env = "msvc", not(target_arch = "arm")))], so I will test it on another windows device later today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried ./x b library/panic_unwind --stage 3 and it succeed:

Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.17s
Building stage0 library artifacts (x86_64-pc-windows-msvc)
    Finished `release` profile [optimized + debuginfo] target(s) in 0.23s
Building compiler artifacts (stage0 -> stage1, x86_64-pc-windows-msvc)
    Finished `release` profile [optimized + debuginfo] target(s) in 1.09s
Creating a sysroot for stage1 compiler (use `rustup toolchain link 'name' build/host/stage1`)
Building stage1 library artifacts (x86_64-pc-windows-msvc)
    Finished `release` profile [optimized + debuginfo] target(s) in 0.36s
Building compiler artifacts (stage1 -> stage2, x86_64-pc-windows-msvc)
    Finished `release` profile [optimized + debuginfo] target(s) in 0.83s
Creating a sysroot for stage2 compiler (use `rustup toolchain link 'name' build/host/stage2`)
Uplifting library (stage1 -> stage2)
Uplifting rustc (stage1 -> stage3)
Creating a sysroot for stage3 compiler (use `rustup toolchain link 'name' build/host/stage3`)
Uplifting library (stage1 -> stage3)
Build completed successfully in 0:00:10

@fee1-dead
Copy link
Member

r? @compiler-errors

@rustbot rustbot assigned compiler-errors and unassigned fee1-dead Oct 13, 2024
@mu001999
Copy link
Contributor Author

@compiler-errors friendly ping :)

@compiler-errors
Copy link
Member

r? compiler

@rustbot rustbot assigned estebank and unassigned compiler-errors Dec 16, 2024
@estebank
Copy link
Contributor

Based on my understanding of the code and https://github.com/rust-lang/rust/pull/131439/files?diff=split&w=1#r1798908816,

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 31, 2024

📌 Commit 1e10e50 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 31, 2024
@bors bors merged commit f4db757 into rust-lang:master Jan 1, 2025
6 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Jan 1, 2025
@mu001999 mu001999 deleted the cleanup/static-mut branch January 1, 2025 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants